package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementProduct;
import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementProductRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementProductRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementProductResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.XHH_LOAN_SETTLEMENT_COMPANY;
import static com.tuanzi.loan.generated.jooq.loan.Tables.XHH_LOAN_SETTLEMENT_PRODUCT;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductInfo.LOAN_PRODUCT_INFO;

/**
 * @author caiwanlin
 * @create 2018-08-16 16:21
 */
public class XhhSettlementProductRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementProductRepositoryCustom {
	@Override
	protected TableImpl getTable() {
		return XHH_LOAN_SETTLEMENT_PRODUCT;
	}

	@Override
	public Page<XhhSettlementProductResponse> list(XhhSettlementProductRequest searchRequest, Pageable pageable) {
		Integer billProject = searchRequest.getBillProject();
		String productName = searchRequest.getProductName();
		Integer copMode = searchRequest.getCopMode();
		Integer spreadChannel = searchRequest.getSpreadChannel();
		Integer spreadMode = searchRequest.getSpreadMode();
		Integer settlementMode = searchRequest.getSettlementMode();
		String creator = searchRequest.getCreator();
		Date startTime = searchRequest.getStartTime();
		Date endTime = searchRequest.getEndTime();
		if (endTime != null) {
			endTime = DateUtils.addHours(endTime, 23);
			endTime = DateUtils.addMinutes(endTime, 59);
			endTime = DateUtils.addSeconds(endTime, 59);
		}
		fields(XHH_LOAN_SETTLEMENT_PRODUCT.fields());
		fields(XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT);
		leftJoin(XHH_LOAN_SETTLEMENT_COMPANY, XHH_LOAN_SETTLEMENT_PRODUCT.COMPANY_ID.eq(XHH_LOAN_SETTLEMENT_COMPANY.ID));
		eq(XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT, billProject);
		contains(XHH_LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME, productName);
		eq(XHH_LOAN_SETTLEMENT_PRODUCT.COP_MODE, copMode);
		eq(XHH_LOAN_SETTLEMENT_PRODUCT.SETTLEMENT_MODE,settlementMode);
		eq(XHH_LOAN_SETTLEMENT_PRODUCT.SPREAD_CHANNEL, spreadChannel);
		eq(XHH_LOAN_SETTLEMENT_PRODUCT.SPREAD_MODE, spreadMode);
		eq(XHH_LOAN_SETTLEMENT_PRODUCT.CREATOR, creator);
		between(XHH_LOAN_SETTLEMENT_PRODUCT.CREATE_TIME, startTime, endTime);
		orderBy(XHH_LOAN_SETTLEMENT_PRODUCT.CREATE_TIME, Sort.Direction.DESC);
		Page<XhhSettlementProductResponse> search = search(pageable, record -> {
			XhhSettlementProductResponse response = new XhhSettlementProductResponse();
			response.setId(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.ID));
			response.setProductName(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME));
			response.setSpreadChannel(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.SPREAD_CHANNEL));
			response.setSpreadMode(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.SPREAD_MODE));
			response.setCopMode(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.COP_MODE));
			response.setPriceCpa(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.PRICE_CPA));
			response.setPriceCps(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.PRICE_CPS));
			response.setSettlementMode(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.SETTLEMENT_MODE));
			response.setIncomeType(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.INCOME_TYPE));
			response.setActivityIdStr(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.ACTIVITY_ID_STR));
			response.setCompanyId(record.get(XHH_LOAN_SETTLEMENT_PRODUCT.COMPANY_ID));
			response.setRemark(record.get(XHH_LOAN_SETTLEMENT_COMPANY.REMARK));
			response.setCreator(record.get(XHH_LOAN_SETTLEMENT_COMPANY.CREATOR));
			response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_COMPANY.CREATE_TIME));
			response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_COMPANY.UPDATOR));
			response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_COMPANY.UPDATE_TIME));
			response.setBillProject(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT));
			return response;
		});
		return search;
	}

	@Override
	public List<ProductInfo> getActivityList() {
		List<ProductInfo> productInfoList = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME).from(LOAN_PRODUCT_INFO).fetchInto(ProductInfo.class);
		return productInfoList;
	}

	@Override
	public List<XhhSettlementProduct> getProductList() {
		fields(XHH_LOAN_SETTLEMENT_PRODUCT.ID, XHH_LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME);
		List<XhhSettlementProduct> search = search(XhhSettlementProduct.class);
		return search;
	}
}
